<template>
  <view>
    <view class="settle-box">
      <label class="radio" @click="clickto">
        <radio :checked="statusSum" color="#c00000" /><text>全选</text>
      </label>
      <view class="settle-txt">
        <text>合计: ￥<text>{{priceSum}}</text></text>
      </view>
      <button @click="settlement" class="settle-btn">结算({{totalSum}})</button>
    </view>
  </view>
</template>

<script>
  import $http from '/utils/request.js';
  import { useUserStore } from '../../store/user';
  import { useCartStore } from '../../store/store';
  const cartStore = useCartStore()
  const userStore = useUserStore()
  export default {
    name:"my-settle",
    props: {
      priceSum: {
        type: Number,
        default: 0
      },
      sumNum: {
        type: Number,
        default: 0
      }
    },
    data() {
      return {
        totalSum: cartStore.statesSum,
        statusSum: cartStore.totalSum,
        token: userStore.token,
        sum: 3
      };
    },
    methods: {
      clickto() {
        // console.log(val);  
        cartStore.clickTotal(!cartStore.totalSum)
      },
      // 结算
      settlement() {
        // 是否选择商品
        if (!this.totalSum) return uni.showToast({
          title: '请先选择商品',
          icon: 'none'
        })
        //地址
        // if ()
        // 登入后跳转回原页面
        if (!this.token) return this.delaNavigate()
        
        // 生成订单
        this.payOrder()
      },
      // 登入后跳转回原页面
      delaNavigate() {
        this.showTips(this.sum)
        let a = setInterval(() => {
          this.sum-=1
          if (this.sum <= 0) {
            clearInterval(a)
            uni.switchTab({
              url: '/pages/my/my',
              success: () => {
                userStore.updateRedirectInfo({
                  openType: 'switchTab',
                  from: '/pages/cart/cart'
                })
              }
            })
            this.sum = 3
            return
          }
          this.showTips(this.sum)
        },1000)
      },
      showTips(n) {
        uni.showToast({
          title: `请先登录, ${n}后自动跳转登入页`,
          icon: 'none',
          mask: true,
          duration: 1500
        })
      },
      async payOrder() {
        // 准备订单信息对象
        const orderInfo = {
          // cartStore.priceSum
          order_price: 0.01,
          consignee_addr: userStore.address,
          goods: cartStore.cart.filter(item => item.state === 1).map(item => ({
            goods_id: item.id,
            goods_number: item.count,
            goods_price: item.price,
          }))
        }
        
        // 发起请求创建订单
        const { data } = await uni.$http.post('api/public/v1/my/orders/create', orderInfo)
        console.log(data);
        if (data.meta.ststus !== 200) return uni.showToast({
          title:'创建订单失败',
          icon: 'none'
        })
        
        // 得到服务器响应的编号
        const orderNum = data.message.order_number
        console.log(order);
        
        // const data = await uni.$http.post('api/public/v1/my/orders/req_unifiedorder', { order_number: orderNum})
        // if (data.data.meta.states !== 200) return uni.showToast({
        //   title: '生成预付订单失败',
        //   icon: 'none'
        // })
        // // 得到支付相关的必要参数
        // const payInfo = data.message.pay
        // // 发起微信支付
        // const res = uni.requestPayment(payInfo)
        // const data = await uni.$http.post('api/public/v1/my/orders/chkOrder', { order_number: orderNum })
        // if (data.data.meta.states !== 200) return uni.showToast({
        //   title: '订单未支付',
        //   icon: 'none'
        // })
        // uni.showToast({
        //   title: "支付完成"
        // })
      }
    },
    onLoad() {
      // this.totalSum = cartStore.totalSum
    }
  }
</script>

<style lang="scss">
.settle-box {
  width: 100vw;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 5px;
  font-size: 14px;
  background-color: #fff;
  label {
    margin-top: 3px;
  }
  text {
    text {
    color: #c00000;
  }
  }
  .settle-btn {
      margin: 0;
      width: 120px;
      height: 60px;
      text-align: center;
      line-height: 60px;
      background-color: #c00000;
      color: #fff;
      font-size: 14px;
      border-radius: 0;
  }
}
</style>